/**
 * OA项目辅助模块统一配置文件
 * 作为统一入口，导出所有辅助模块
 */

// 导入路径解析器
const pathResolver = require('./path_resolver.js');

// 从全局模块配置中导入模块
const moduleConfig = require('../../../config/module_config.js');
const { Helpers, NpmPackages } = moduleConfig;

// 解构全局助手模块
const {
  pageHelper,
  cloudHelper,
  timeHelper,
  fileHelper,
  routerHelper,
  dataHelper,
  validate,
  resourcesStore
} = Helpers;

// 解构npm包
const {
  mobx, 
  createStoreBindings,
  dayjs
} = NpmPackages;

// 确保获取cacheHelper
let cacheHelper;
try {
  // 直接导入cacheHelper以确保可用
  cacheHelper = require('../../../helpers/cache_helper.js');
  console.log('[helpers_config] 成功直接导入cacheHelper');
} catch (e) {
  console.error('[helpers_config] 导入cacheHelper失败，创建备用实现:', e.message);
  // 创建备用实现
  cacheHelper = {
    get: function(key, def = null) {
      console.warn(`[cacheHelper备用] get: ${key}`);
      const value = wx.getStorageSync(key);
      return value !== '' ? value : def;
    },
    set: function(key, value, expire = 86400) {
      console.warn(`[cacheHelper备用] set: ${key}`);
      wx.setStorageSync(key, value);
    },
    remove: function(key) {
      console.warn(`[cacheHelper备用] remove: ${key}`);
      wx.removeStorageSync(key);
    },
    has: function(key) {
      console.warn(`[cacheHelper备用] has: ${key}`);
      return wx.getStorageSync(key) !== '';
    },
    CACHE_GROUP: {
      SYSTEM: 'system',
      USER: 'user',
      LIST: 'list',
      DETAIL: 'detail',
      CONFIG: 'config',
      TEMP: 'temp'
    },
    CACHE_PRIORITY: {
      HIGH: 3,
      NORMAL: 2,
      LOW: 1
    }
  };
}

// 直接定义默认的项目特有助手模块
const requestHelper = {
  get: async () => ({}),
  post: async () => ({})
};

const timerHelper = {
  format: (time) => time,
  timestamp: () => Date.now()
};

// 尝试导入项目业务逻辑模块
let ProjectBiz, FlowBiz;

try {
  const bizConfig = require('./biz_config.js');
  FlowBiz = bizConfig.FlowBiz;
  console.log('[helpers_config] 成功导入FlowBiz');
} catch (e) {
  console.error('[helpers_config] 导入FlowBiz失败:', e.message);
  // 提供默认实现
  FlowBiz = {
    forceSetLoginState: function() { console.warn('FlowBiz.forceSetLoginState - 模块未正确加载'); },
    loadDetail: function() { console.warn('FlowBiz.loadDetail - 模块未正确加载'); },
    getCateMenu: () => []
  };
}

try {
  ProjectBiz = require('../comm/biz/project_biz.js');
  console.log('[helpers_config] 成功导入ProjectBiz');
} catch (e) {
  console.error('[helpers_config] 导入ProjectBiz失败:', e.message);
  // 提供默认实现
  ProjectBiz = {
    initPage: () => {},
    getProjectColor: () => 'blue'
  };
}

// 使用本地的直接定义，而不是从外部路径导入
const PassportBiz = require('./passport_biz_config.js');
console.log('[helpers_config] 成功导入PassportBiz');

// 导出所有辅助模块
module.exports = {
  // 路径解析器
  pathResolver,
  
  // 全局助手模块
  pageHelper,
  cloudHelper,
  timeHelper,
  fileHelper,
  routerHelper,
  dataHelper,
  validate,
  cacheHelper,
  
  // 项目特有助手模块
  requestHelper,
  timerHelper,
  
  // 业务逻辑模块
  ProjectBiz,
  FlowBiz,
  PassportBiz,
  
  // NPM包相关
  mobx,
  createStoreBindings,
  dayjs,
  
  // 资源存储
  resourcesStore,
  
  /**
   * 获取全局助手模块
   * @param {string} context - 上下文环境，可选值：page、service
   * @returns {Object} 助手模块集合
   */
  getGlobalHelpers: function(context = 'page') {
    return {
      pageHelper,
      cloudHelper,
      timeHelper,
      validate,
      routerHelper,
      fileHelper,
      dataHelper,
      cacheHelper,
      createStoreBindings,
      mobx,
      dayjs,
      ProjectBiz,
      FlowBiz,
      PassportBiz
    };
  }
}; 